<%
if (session.getAttribute("username") == null) {
	%><jsp:forward page="../login.jsp" /><%
}
System.out.println(session.getAttribute("pollid").toString());
%>

<%@page language="java" import="java.util.*, model.*" pageEncoding="ISO-8859-1"%>
<%@page import="bean.Poll,bean.Question,connection.DbConnection,java.sql.*"%>
<%

String pid = (String)session.getAttribute("pollid");
Integer pver = Integer.parseInt(session.getAttribute("ver").toString()); 
String pname = session.getAttribute("pname").toString();
String pdes = session.getAttribute("des").toString();

System.out.println("pid:"+pid+"pver:"+pver+"pname"+pname+"pdes"+pdes);

String mcadd = (String)session.getAttribute("mcadd");
boolean addoptions = false;
if(mcadd != null){
	addoptions = true;
	session.removeAttribute("mcadd");
}

int optionid = 0;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>   
<link rel="stylesheet" href="/DBP/style/stylesheet.css" type="text/css" >
    <title><%= pname%>'s Information</title> 
    <script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript" src="scriptaculous.js"></script>
	<script type="text/javascript">
	var optionid = 0;
	document.observe("dom:loaded", bodyOnload);
    function bodyOnload() {
    $('Basic').show();
    $('Display').hide();
	$('Objects').hide();
	$('saBasic').show();
    $('saDisplay').hide();
	$('saObjects').hide();
}

	function transition(show, hide){
		Effect.Appear(show);
		Effect.Fade(hide, { duration:0.1});
		if(show == 'MC'){
			toggleQuestionType("MC");
		}else{
			toggleQuestionType("SA");
		}
	}
	
	document.observe('dom:loaded', function()
	{$('SA').hide();
	});
	function changeFont()
	{
		var text = document.getElementById("text").value;
		var font = document.getElementById("font").value;
		document.getElementById("text").style.fontFamily = font;		
	}
	function changeSize()
	{
		var text = document.getElementById("text").value;
		var size = document.getElementById("size").value;
		document.getElementById("text").style.fontSize = size;		
	}
	function changeColor()
	{
		var text = document.getElementById("text").value;
		var color = document.getElementById("color").value;
		document.getElementById("text").style.color = color;		
	}
	function Alphanumaric()
	{
	document.getElementById("Numaric").style.display = "none";
	document.getElementById("Alphanumaric").style.display = "";
	}
	function Numaric()
	{
	document.getElementById("Numaric").style.display = "";
	document.getElementById("Alphanumaric").style.display = "none";
	}
	function Single()
	{
	document.getElementById("Range").style.display = "none";
	document.getElementById("Single").style.display = "";
	document.getElementById("Single").disabled = false;
	document.getElementById("Range").disabled = true;
	}
	function Range()
	{
	document.getElementById("Range").style.display = "";
	document.getElementById("Single").style.display = "none";
	document.getElementById("Single").disabled = true;
	document.getElementById("Range").disabled = false;
	}
	</script>
	<script type="text/javascript" language="javascript">

    var currentId = null;
    var currentIdd = null;
    var currentIddd = null;
    var currentIdddd = null;

    Effect.ShowHide = function (contentId) {
    	var slideDown = 0.5;
    	var slideUp = 0.5;

    	contentId = $(contentId);

    	if (currentId != contentId) {
    		new Effect.SlideDown(contentId, {duration: slideDown});
    		currentId = contentId;
    	} else {
    		new Effect.SlideUp(currentId, {duration: slideUp});
    		currentId = null;
    	}
    };

    function check(type){
        //alert(type);
		if(type=='mc'){
			var text = document.getElementById("mctext");
			//alert(text.value);
			if(text.value == ''){
				document.getElementById("mctexterror").style.display = "block";
				return false;
			}else{
				//for each option entered:
				//1) add (text-correct-points) to optionslist, comma separated
				//1b) if text is empty, ret false
				//2) profit?
				var ops = document.getElementsByTagName("input"); 	
				document.getElementById("optionslist").value = "";
				for(var i = 0; i < ops.length; i++){
					var elem = ops[i]
					var str = "";
					if(elem.id.slice(0, 6)=="optext"){
						if(elem.value == ""){
							return false;
						}
						str+=elem.value+"-";
					}else if(elem.id.slice(0, 5)=="opcor"){
						if(elem.checked == true){
							str+="true"+"-";
						}else{
							str+="false"+"-";
						}
					}else if(elem.id.slice(0, 5)=="oppoi"){
						if(elem.value == ""){
							str+="0,";
						}else{
							str+=elem.value+",";
						}
					}else{
						continue;
					}
					document.getElementById("optionslist").value += str;
				}
				if(document.getElementById("optionslist").value != ""){
					return true;
				}else{
					return false;
				}
			}
		}else{
			var text = document.getElementById("satext");
			//alert(text.value);
			if(text.value == ''){
				document.getElementById("satexterror").style.display = "block";
				return false;
			}else{
				return true;
			}
		}
    }
    
    Effect.ShowHidee = function (contentId) {
    	var slideDown = 0.5;
    	var slideUp = 0.5;

    	contentIdd = $(contentId);

    	if (currentIdd != contentId) {
    		new Effect.SlideDown(contentId, {duration: slideDown});   			
    		currentIdd = contentId;
    	} else {
    		new Effect.SlideUp(currentIdd, {duration: slideUp});
    		currentIdd = null;
    	}
    };
    
    Effect.ShowHideee = function (contentId) {
    	var slideDown = 0.5;
    	var slideUp = 0.5;

    	contentIddd = $(contentId);

    	if (currentIddd != contentId) {
    		new Effect.SlideDown(contentId, {duration: slideDown});   			
    		currentIddd = contentId;
    	} else {
    		new Effect.SlideUp(currentIddd, {duration: slideUp});
    		currentIddd = null;
    	}
    };
    
    Effect.ShowHideeee = function (contentId) {
    	var slideDown = 0.5;
    	var slideUp = 0.5;

    	contentIdddd = $(contentId);

    	if (currentIdddd != contentId) {
    		new Effect.SlideDown(contentId, {duration: slideDown});   			
    		currentIdddd = contentId;
    	} else {
    		new Effect.SlideUp(currentIdddd, {duration: slideUp});
    		currentIdddd = null;
    	}
    };
    
	function toggleQuestionType(e){
		var qbox = document.getElementById("qbox").innerHTML;
		if(e == "MC"){
			document.getElementById("qbox").innerHTML = "New Multiple Choice Question";
		}else{
			document.getElementById("qbox").innerHTML = "New Short Answer Question";
		}
	}
	
	String.prototype.trim = function() {
		   return this.replace(/^\s+|\s+$/g,"");
	}
	
	function addOption(){
		optionid++;
		if((optionid > 0)&&(optionid <= 10)){
			var str = ("" + optionid).trim();
			document.getElementById("optionsdiv").innerHTML += "<tr id = \"tr"+optionid+"\"><td width=\"2px\">"+str+"</td><td><input type=\"text\" id=\"optext"+str+"\" name=\"option\"/></td><td style=\"width:4%\"><input type=\"checkbox\" id=\"opcor"+str+"\" name=\"optioncorrect\" /></td><td><input type=\"text\" id=\"oppoi"+str+"\" name=\"optionpoints\"/></td><td><input type=\"button\" class=\"button\" value=\"Remove\" onClick=\"removeOption('"+str+"')\"/></td></tr>";
		}
	}
	
	function removeOption(removeid){
		var d = document.getElementById("optionsdiv");
		var olddiv = document.getElementById("tr"+removeid);
		d.removeChild(olddiv);
		optionid--;
	}
    
</script>
</head>
<body>	
	<div class="bg">
	<div class="content">
	<jsp:include page="../nav.jsp" />
	<div class= "pagetitle">Poll Creation > Edit questions in "<%= pname%>"</div>
	<div class="entrytitle">
	Questions
	<div class="entry">
	<div id="Information">		
	<div id="goods">
	<form name = "aa" action = "DeleteQuestionServlet" method="post">
			 <div class="tableborder"><table class="blockTable">
				<thead>
				<tr id="row1" align="left">
				<th>Text</th>
				<th>Type</th>
				<th>CreateDate</th>
				<th></th><th></th>
				</tr>
				</thead>
				<tbody>
				<%
				DbConnection db = new DbConnection();
				Connection conn = db.getConn();
				DbConnection db2 = new DbConnection();
				Connection conn2 = db2.getConn();
				
		    	PreparedStatement st = conn.prepareStatement("select question.questionid, question.text, question.qcreatetime from question, Poll_Question where question.questionid = Poll_Question.questionid and Poll_Question.pollid = " + pid );
				ResultSet rs=st.executeQuery();
				Question pb = new Question();
				boolean test = false;
				while(rs.next()){
					test = true;
		    		String type = "";
		    		String sql = "SELECT * FROM Options WHERE QuestionID = "+rs.getInt(1);
		    		System.out.println("ResultSet:");
		    		PreparedStatement ps = conn2.prepareStatement(sql);
		    		if(ps.executeQuery().next()){
		    			type = "Multiple Choice";
		    		}else{
		    			type = "Short Answer";
		    		}
		    		%>
		    		<tr>		
					<td><%=rs.getString(2)%></td>
					<td><%=type %></td>
					<td><%=rs.getDate(3)%></td>
					<td>
						<a href="Addquestion.jsp?editid=<%=rs.getString(1)%>">
							<input type="button" class="button" value="Edit" />
						</a>
						<a href="deleteQuestion.jsp?id=<%=rs.getString(1)%>&text="+rs.getString(2)>	
							<input type="button" class="button" id="warning"  value="Delete" />
						</a>
					</td>
					</tr>
		    		<%
		    		System.out.println(rs.getInt(1));
		    		System.out.println("showq.java qid: "+pb.getQuestionID());
	    		}
				if(test == false){
					%>
					<tr>
					<td>There are no questions in this poll.</td>
					<td></td>
					<td></td>
					<td></td>
					</tr>
					<%
				}
				db.close();
				db2.close();
				%>

				</tbody>
			</table> 
			</div>
			</form>
			<%if(request.getParameter("deletefail")!=null && request.getParameter("deletefail").equals("false")){ 
				String deltext = request.getParameter("deletetext");
				if(deltext != null){
			%>
				<div class="note">"<%=deltext.toString()%>" deleted.</div>
				<%}else{ %>
				<div class="note">Question deleted.</div>
				<%}%>
			<%} %>
		</div >	
		</div>
		</div></div>
		<div class="entrytitle" ><span id="qbox">New Multiple Choice Question</span><div class="entry">
		<a href="#" onclick="transition('MC', 'SA')"><div class="button" style="float:left; width:70px; margin-right:10px;">Multiple choice</div>	</a>
		<a href="#" onclick="transition('SA', 'MC')"><div class="button" style="width:70px; ">Short Answer</div></a>
	<br/>
	<div id="SA">
	<form name = "sa" action = "AddSQuestionServlet" method="post">
		<div id="saBasic">
		Text:	<input type = "text" name = "text" id = "satext"  size="50"/><br/><div class="error" id="satexterror">Please give a title for your question</div>
				<br/>
		Question Type: 	<input   type= "radio"   value= "0" checked="true" name= "numberonly" onClick="Alphanumaric()">Alphanumeric 
						<input   type= "radio"   value= "1"  name= "numberonly" onClick="Numaric()">Numeric
						<br/>
		<div id="Alphanumaric">
			Correct Answer: <input type = "text" name = "Correct1" id = "Correct1" size="10"/>
						<input type = "text" name = "Correct2" id = "Correct2" size="10"/>
						<input type = "text" name = "Correct3" id = "Correct3" size="10"/>
						<input type = "text" name = "Correct4" id = "Correct4" size="10"/>
						<input type = "text" name = "Correct5" id = "Correct5" size="10"/>
		</div>
		<div id="Numaric" style="display:none">
		Type:	<input   type= "radio"   value= "Single" checked="true"name= "ntype" onClick="Single()">Single 
				<input   type= "radio"   value= "Range"  name= "ntype" onClick="Range()"> Range
				&nbsp &nbsp
				<div id="Single">Correct Answer:<input type = "text" name = "numberonly" id = "numberonly" size="10"/></div>
		<div id="Range" style="display:none"  disabled="true" >Lowest: <input type = "text" name = "lowest" id = "lowest" size="10"/>
		Highest: <input type = "text" name = "highest" id = "highest" size="10"/></div>
		</div>
		</div>
		<div  onclick="new Effect.ShowHideee('saDisplay');" style="background:#03387F;color:#fff;text-align:center;">v Style Settings v</div>
		<div id="saDisplay">
		Font: 	<select id="font" name="font" onchange="changeFont()" >	 	
				<option value="Arial">Arial</option>
				<option value="Comic Sans MS"> Comic Sans MS</option>
				<option value="Courier New"> Courier New</option>
				<option value="Georgia"> Georgia</option>
				<option value="Helvetica"> Helvetica</option>
				<option value="Lucida Grande"> Lucida Grande</option>
				<option value="Impact"> Impact</option>
				<option value="Tahoma"> Tahoma</option>	
				<option value="Times New Roman" > Times New Roman</option>	
				<option value="Trebuchet MS"> Trebuchet MS</option> 
				<option value="Verdana" selected="selected"> Verdana </option>		
				</select>
				&nbsp &nbsp
		Size:	<select id="size" name="size" onchange="changeSize()" >	
				<option value="9"> 9</option>
				<option value="10"> 10</option>
				<option value="11"> 11</option>
				<option value="12"> 12</option>
				<option value="13" selected="selected"> 13</option>
				<option value="14"> 14</option>
				<option value="16"> 16</option>
				<option value="18"> 18</option>
				<option value="20"> 20</option> 
				<option value="22"> 22</option>
				<option value="24"> 24</option>
				<option value="26"> 26</option>
				<option value="28"> 28</option>
				<option value="36"> 36</option>
				<option value="48"> 48</option>
				<option value="72"> 72</option>		
				</select>
				&nbsp &nbsp
		Color: <select id="color" name="color" onchange="changeColor()" >	
				<option value="Black" selected="selected"> Black</option>
				<option value="DarkRed"> DarkRed</option>
				<option value="Red"> Red</option>
				<option value="Orange"> Orange</option>
				<option value="Yellow"> Yellow</option>
				<option value="LightGreen"> LightGreen</option>
				<option value="Green"> Green</option>
				<option value="LightBlue"> LightBlue</option>
				<option value="Blue"> Blue</option>
				<option value="DarkBlue"> DarkBlue</option>
				<option value="Purple" > Purple</option>
				</select>
				<br/>
		</div>
		<div  style="margin-top:1px;background:#03387F;color:#fff;text-align:center;" onclick="new Effect.ShowHideeee('saObjects');">v Interactive Objects v</div>
		<div id = "saObjects">
		Timer:	<select id="timer" name="timer" >
				<option value="0" selected="selected"> No Timer </option>	
				<option value="10"> 10</option>
				<option value="15"> 15</option>
				<option value="20"> 20</option>
				<option value="25"> 25</option>
				<option value="30"> 30</option>
				<option value="35"> 35</option>
				<option value="40"> 40</option>
				<option value="45"> 45</option>
				<option value="50"> 50</option> 
				<option value="55"> 55</option>
				<option value="60"> 60</option>
				<option value="75"> 75</option>
				<option value="90"> 90</option>
				<option value="120"> 120</option>
				<option value="150"> 150</option>
				<option value="180"> 180</option>
				</select><br/>
		Response Counter: 			<input   type= "radio"   value= "Y" name= "RC">Yes 
									<input   type= "radio"   value= "N" checked="true"  name= "RC">No
									<br/>
		Correct Answer Indicator: 	<input   type= "radio"   value= "Y" name= "CAI">Yes 
									<input   type= "radio"   value= "N" checked="true"  name= "CAI">No
		
		</div>
		<input type = "hidden" name = "pid" value="<%=pid%>"></input>
	<input type = "hidden" name = "pver" value="<%=pver%>"></input>
	<input type = "hidden" name = "pname" value="<%=pname%>"></input>
	<input type = "hidden" name = "pdes" value="<%=pdes%>"></input>
	<input type = "hidden" name = "TemplateQuestion" value="0" />
	<input type="submit" class="button" onClick="return check('sa')" value="Add Question"/>
	</form>
	</div>
	<div id="MC">
		<form name = "mc" action = "AddMQuestionServlet" method="post">
		<div id="Basic">
		Text:	<input type = "text" name = "text" id = "mctext" size="50"/><br/><div class="error" id="mctexterror">Please give a title for your question</div>
				<br/>
		<span style="color:#aaa">Type:	<input   type= "radio"   value= "Normal"   checked="true"  name= "type">Normal
				<input   type= "radio"   value= "Demographic" 	name= "type">Demographic
				<input   type= "radio"   value= "Comparative"   name= "type">Comparative 
				<input   type= "radio"   value= "Ranking"   name= "type">Ranking
				<br/>
				</span>
		

		
	
	 
	 <%//OPTIONS %>
		<br/>Add options to question
		<div class="tableborder"><table class="blockTable">
			<thead>
			<tr>
				<td width="2px"></td>
				<td>Text</td>
				<td>Correct Answer</td>
				<td>Points</td>
				<td></td>
			</tr>
			</thead>
			<tbody id="optionsdiv">
			<tr>
			<td><div class="button" onClick="addOption()">+ Add option</div></td><td></td><td></td><td></td></tr>
			</tbody>
		</table> 
		</div>
		</div>
		<div style="background:#03387F;color:#fff;;text-align:center;"onclick="new Effect.ShowHide('Display');">v Style Settings v</div>
		<div id="Display">
		Font: 	<select id="font" name="font" onchange="changeFont()" >	 	
				<option value="Arial">Arial</option>
				<option value="Comic Sans MS"> Comic Sans MS</option>
				<option value="Courier New"> Courier New</option>
				<option value="Georgia"> Georgia</option>
				<option value="Helvetica"> Helvetica</option>
				<option value="Lucida Grande"> Lucida Grande</option>
				<option value="Impact"> Impact</option>
				<option value="Tahoma"> Tahoma</option>
				<option value="Times New Roman" > Times New Roman</option>	
				<option value="Trebuchet MS"> Trebuchet MS</option> 
				<option value="Verdana" selected="selected"> Verdana </option>		
				</select>
				&nbsp &nbsp
		Size:	<select id="size" name="size" onchange="changeSize()" >	
				<option value="9"> 9</option>
				<option value="10"> 10</option>
				<option value="11"> 11</option>
				<option value="12"> 12</option>
				<option value="13" selected="selected"> 13</option>
				<option value="14"> 14</option>
				<option value="16"> 16</option>
				<option value="18"> 18</option>
				<option value="20"> 20</option> 
				<option value="22"> 22</option>
				<option value="24"> 24</option>
				<option value="26"> 26</option>
				<option value="28"> 28</option>
				<option value="36"> 36</option>
				<option value="48"> 48</option>
				<option value="72"> 72</option>		
				</select>
				&nbsp &nbsp
		Color: <select id="color" name="color" onchange="changeColor()" >	
				<option value="Black" selected="selected"> Black</option>
				<option value="DarkRed"> DarkRed</option>
				<option value="Red"> Red</option>
				<option value="Orange"> Orange</option>
				<option value="Yellow"> Yellow</option>
				<option value="LightGreen"> LightGreen</option>
				<option value="Green"> Green</option>
				<option value="LightBlue"> LightBlue</option>
				<option value="Blue"> Blue</option>
				<option value="DarkBlue"> DarkBlue</option>
				<option value="Purple" > Purple</option>
				</select>
				<br/>
		Chart Style: <select id="chartstyle" name="chartstyle" >	
				<option value="Vertical" selected="selected"> Vertical</option>
				<option value="Horizontal"> Horizontal</option>
				<option value="Pie"> Pie</option>
				</select>
				&nbsp &nbsp	
		</div>
		<div  style="margin-top:1px;background:#03387F;color:#fff;text-align:center;" onclick="new Effect.ShowHidee('Objects');">v Interactive Objects v</div>
		<div id = "Objects">
		Timer:	<select id="timer" name="timer" >
				<option value="0" selected="selected"> No Timer </option>	
				<option value="10"> 10</option>
				<option value="15"> 15</option>
				<option value="20"> 20</option>
				<option value="25"> 25</option>
				<option value="30"> 30</option>
				<option value="35"> 35</option>
				<option value="40"> 40</option>
				<option value="45"> 45</option>
				<option value="50"> 50</option> 
				<option value="55"> 55</option>
				<option value="60"> 60</option>
				<option value="75"> 75</option>
				<option value="90"> 90</option>
				<option value="120"> 120</option>
				<option value="150"> 150</option>
				<option value="180"> 180</option>
				</select><br/>
		Response Counter: 			<input   type= "radio"   value= "Y" name= "RC">Yes 
									<input   type= "radio"   value= "N" checked="true" name= "RC">No
									<br/>
		Correct Answer Indicator: 	<input   type= "radio"   value= "Y" name= "CAI">Yes 
									<input   type= "radio"   value= "N" checked="true" name= "CAI">No
		
		</div>
	    <input type = "hidden" name = "optionslist" id="optionslist" value=""></input>
	    <input type = "hidden" name = "TemplateQuestion" value="0" />
		<input type="submit" onClick="return check('mc')" class="button" value="Add Question"/>
		</form>
		</div>
		</div>
		</div>
</body>
</html>
	 	 
	